using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace XMas
{
    
    /// <summary>
    ///  Base class for an Item of AnimationSystem
    /// </summary>
    [Serializable()]    
    public class AnimationNodeItem 
    {
        /// <summary>
        /// Text show in editor
        /// </summary>
        private string _strText;        
        /// <summary>
        /// Id destination node, for serialization
        /// </summary>
        private System.Guid _IDdestination;             
        /// <summary>
        /// Cached destination node for speed
        /// </summary>        
        private AnimationNode _AnimNodeDestination; 
        /// <summary>
        /// Local weigth of this item
        /// </summary>
        public float _fLocalWeigth;
        /// <summary>
        /// Acumulate weigth of this item
        /// </summary>
        public float _fAcumWeigth;
        /// <summary>
        /// Number of version of this node, used in serialization
        /// </summary>
        /// <remarks>
        /// If you add a new value to serialization, you need to increment this float
        /// This is need to have compatibility with olders version of library
        /// <see cref="WriteXml(System.Xml.XmlWriter)"/>
        /// </remarks>  
        protected const float _SerializationVersion = 1.0f;
        

        #region Properties

        /// <summary>
        /// Atribute for _strText
        /// <see cref="AnimationNodeItem._strText"/>
        /// </summary>
        [CategoryAttribute("AnimNodeItem"), DescriptionAttribute("Text")]
        public string Text
        {
            get { return _strText; }
            set { _strText = value; }
        }

        /// <summary>
        /// Atribute for _IDdestination
        /// <see cref="AnimationNodeItem._IDdestination"/>
        /// </summary>
        [Browsable(false)]
        public System.Guid DestinationGuid
        {
            get { return _IDdestination; }            
        }

        /// <summary>
        /// Atribute for _fLocalWeigth
        /// <see cref="AnimationNodeItem._fLocalWeigth"/>
        /// </summary>
        [Browsable(false)]
        public float LocalWeigth
        {
            get { return _fLocalWeigth; }
        }

        /// <summary>
        /// Atribute for _fAcumWeigth
        /// <see cref="AnimationNodeItem._fAcumWeigth"/>
        /// </summary>
        [Browsable(false)]
        public float AcumWeigth
        {
            get { return _fAcumWeigth; }
        }

        /// <summary>
        /// Atribute for _AnimNodeDestination
        /// <see cref="AnimationNodeItem._AnimNodeDestination"/>
        /// </summary>
        [Browsable(false)]
        public AnimationNode AnimNodeDestination
        {
            get { return _AnimNodeDestination; }
        }

        #endregion


        # region Public Functions

        /// <summary>
        /// Create a connection with node
        /// </summary>
        /// <param name="AnimNode">Node destination</param>
        public void SetConnection(AnimationNode AnimNode)
        {
            if (AnimNode == null)
                _IDdestination = Guid.Empty;
            else
                _IDdestination = AnimNode.ID;
            _AnimNodeDestination = AnimNode;            
        }

        # endregion      


        #region Load/Save XML

        /// <summary>
        /// Function to write in XML format, if you overwrite you must call base class first
        /// </summary>
        /// <remarks>
        /// Be carefully with the save order, it must be the same that read order!!
        /// </remarks>
        /// <param name="writer">Writer to store data</param>
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString("SerializationVersion", _SerializationVersion.ToString(new System.Globalization.CultureInfo("en-US")));
            writer.WriteElementString("Text", _strText);
            writer.WriteElementString("IDDestination", _IDdestination.ToString());
        }

        /// <summary>
        /// Function to read xml format, if you overwrite you must call base class first
        /// </summary>
        /// <remarks>
        /// Be carefully with the read order, it must be the same that save order!!
        /// </remarks>
        /// <param name="reader">Reader to retrieve data</param>
        public void ReadXml(XmlReader reader)
        {
            float SerializationVersion = reader.ReadElementContentAsFloat("SerializationVersion", reader.NamespaceURI);
            _strText = reader.ReadElementContentAsString("Text", reader.NamespaceURI);
            _IDdestination = new Guid(reader.ReadElementContentAsString("IDDestination", reader.NamespaceURI));            
        }

        #endregion


        # region Constructors

        /// <summary>
        /// Public constructor
        /// </summary>
        public AnimationNodeItem()
        {
            // Default values
            _IDdestination = System.Guid.Empty;
            _strText = "Element";
            _fLocalWeigth = 0;
            _fAcumWeigth = 0;
            _AnimNodeDestination = null;
        }

        #endregion
    }
}

    